package com.baizhi.service.impl;

import com.baizhi.mapper.UserMapper;
import com.baizhi.pojos.User;
import com.baizhi.service.LoginService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class LoginServiceImpl extends ServiceImpl<UserMapper,User> implements LoginService {

    /**
     * 登录
     * @param user
     */
    @Override
    public int login(User user) {

        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(User::getUname,user.getUname());

        User one = getOne(wrapper);

        if (one == null){

            return 0;
        }

        String s = DigestUtils.md5DigestAsHex((user.getPassword()+one.getSalt()).getBytes());

        if (s.equals(one.getPassword())){

            return 1;
        }else {

            return 0;
        }
    }
}
